from rest_framework import serializers
from rest_framework.serializers import ModelSerializer

from user.models import User, Department, Rank, Position, Role, Menu, Salary


# 用户
class UserSer(ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'


# 部门
class DepSer(serializers.Serializer):

    # 定义要序列化的字段
    id = serializers.IntegerField(label="部门id", read_only=True)
    # trim_whitespace=True去除首尾空格     validators=[] 对数据校验
    dep_name = serializers.CharField(label="部门名称", max_length=30, min_length=2, default='AI', allow_null=True,
                                     trim_whitespace=True, validators=[])

    # 重写验证数据的方法
    def validate(self, attrs):
        # 实现自己的逻辑
        return attrs

    # 创建数据时，必须重写create方法
    def create(self, validated_data):
        # 自己实现保存
        dep = Department.objects.create(**validated_data)
        return dep

    # 更新数据时,必须重写update方法
    def update(self, instance, validated_data):

        # 更新部门的名称
        instance.dep_name = validated_data.get("dep_name")
        instance.save()
        return instance


# 职级
class RankSerializer(ModelSerializer):
    class Meta:
        model = Rank
        fields = '__all__'


# 岗位
class PositionSerializer(ModelSerializer):
    class Meta:
        model = Position
        fields = '__all__'


# 角色
class RoleSer(ModelSerializer):
    class Meta:
        model = Role
        fields = "__all__"


# 菜单
class MenuSer(ModelSerializer):
    class Meta:
        model = Menu
        fields = '__all__'


# 工资
class SalarySer(ModelSerializer):
    class Meta:
        model = Salary
        fields = '__all__'
